/**
 * Created by henian.xu on 2018/9/4.
 * 公用工具类
 */

class Comm {
    /**
     * 按路径获取对象值
     * @param obj
     * @param path
     * @returns {*}
     */
    getObjectValueByPath(obj, path) {
        if (!obj || !path) return;
        let tempObj = obj;
        path = path.replace(/\[(\w+)\]/g, '.$1');
        path = path.replace(/^\./, '');

        const keyArr = path.split('.');
        for (let i = 0, key; (key = keyArr[i++]); ) {
            if (key in tempObj) {
                tempObj = tempObj[key];
            } else {
                throw new Error('请填写有效的 path!');
            }
        }
        return tempObj;
    }
}

export default new Comm();
export { Comm };
